package com.techindata.cacheredis;


import org.apache.log4j.Logger;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * 
 * @author dongbing
 * 往缓存中存入one key to multi-value 的集合。
 */
public class CacheAdd {
	static Logger logger = Logger.getLogger(CacheAdd.class);
	
	/**
	 * @param key 键
	 * @param member 值
	 * @return 添加成功为true,否则返回false
	 * 默认8小时过期
	 * */
	public static boolean append(String key,String member) {
		return append(key,member,CacheInit.EXPIRE);
	}
	
	
	/**
	 * @param key 键
	 * @param member 值
	 * @param expireSeconds 过期时间
	 * @return 添加成功为true,否则返回false
	 * */
	public static boolean append(String key,String member,int expireSeconds) {
		JedisPool pool = RedisConnectionPool.getPool();
		Jedis jedis=null;
		try {
			jedis= pool.getResource();
            jedis.sadd(key, member);
            if(expireSeconds!=0)jedis.expire(key, expireSeconds);
            return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
            return false;
        }
		finally{
			RedisConnectionPool.returnResource(pool, jedis);
		}
	}
}
